<template>
  <div>
    <el-row type="flex" justify="end">
      <el-select v-model="currentYear" size="small" style="width: 120px" @change="dateChange">
        <el-option v-for="item in yearList" :key="item" :label="item" :value="item" />
      </el-select>
      <el-select v-model="currentMonth" size="small" style="width: 120px;margin-left:10px" @change="dateChange">
        <el-option v-for="item in 12" :key="item" :label="item + '月'" :value="item" />
      </el-select>
    </el-row>

    <!-- 下方日历 -->
    <el-calendar v-model="currentDate">
      <template #dateCell="{date,data}">
        <div class="date-content">
          <span class="text">{{ data.day | getDay }}</span>
          <span v-if="isWeekend(date)" class="rest">休</span>
        </div>
      </template>
    </el-calendar>
  </div>
</template>

<script>
export default {
  // 过滤器
  filters: {
    getDay(oldval) {
      const newDate = oldval.split('-')[2]
      //  startswith 以0开头 的 截取出下标为1的数字 否则 显示原来的
      return newDate.startsWith('0') ? newDate.substr(1) : newDate
    }
  },
  data() {
    return {
      yearList: [], // 年列表
      currentYear: '', // 年
      currentMonth: '', //  月
      currentDate: new Date()// 最新当前时间
    }
  },
  created() {
    this.getYearList()
  },
  methods: {
    //   显示休息
    isWeekend(date) {
      return date.getDay() === 6 || date.getDay() === 0
    },
    dateChange() {
      // 每当年份月份发生变化, 重新创建一个日期替换掉 this.currentDate
    //   new Date('2021+2+1')
      this.currentDate = new Date(`${this.currentYear}- ${this.currentMonth}-1`)
      console.log(this.currentDate)
    },
    getYearList() {
      this.currentYear = this.currentDate.getFullYear() // 当前年
      this.currentMonth = this.currentDate.getMonth() + 1 // 当前月
      //   求一个数组
      //   因为你需要求  前后5年的日期，所以是11 相当于 当前年为一个数字 1   前面5个数 后面5个数 +起来就是11
      //   11次, 2021 + [0 - 11] - 5
      this.yearList = Array.from(Array(11), (value, index) => this.currentYear + index - 5)
    }
  }
}
</script>

<style  scoped>
 /* /deep/ .el-calendar-day {
  height:  auto;
 }
 /deep/ .el-calendar-table__row td,/deep/ .el-calendar-table tr td:first-child,  /deep/ .el-calendar-table__row td.prev{
  border:none;
 } */
.date-content {
  height: 40px;
  text-align: center;
  line-height: 40px;
  font-size: 14px;
}
.date-content .rest {
  color: #fff;
  border-radius: 50%;
  background: rgb(250, 124, 77);
  width: 20px;
  height: 20px;
  line-height: 20px;
  display: inline-block;
  font-size: 12px;
  margin-left: 10px;
}
.date-content .text{
  width: 20px;
  height: 20px;
  line-height: 20px;
 display: inline-block;

}
 ::v-deep .el-calendar-table td.is-selected .text{
   background: #409eff;
   color: #fff;
   border-radius: 50%;
 }
 ::v-deep .el-calendar__header {
   display: none
 }
</style>
